package com.example.sps.controller;

import com.example.sps.commons.Constants;
import com.example.sps.commons.R;
import com.example.sps.pojo.dto.*;
import com.example.sps.pojo.vo.*;
import com.example.sps.utils.JWTUtils;
import com.example.sps.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ProjectName: Sps
 * @Titile: EmpController
 * @Author: nency
 * @Description: TODO
 */
@RestController
@Slf4j
@RequestMapping("/api/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<EmpLoginVO> login(@Validated @RequestBody EmpLoginDTO empLoginDTO){
        EmpLoginVO empLoginVO = employeeService.login(empLoginDTO);
        return R.ok("登录成功",empLoginVO);
    }

    @GetMapping("info")
    public R<EmpInfoVO> info(HttpServletRequest request){
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        return employeeService.info(empToken);
    }

    @PutMapping("/update")
    public R updateEmpInfo(@Validated EmpInfoUpdateDTO empInfoUpdateDTO
            ,HttpServletRequest request){
        log.info("修改个人资料,{}",empInfoUpdateDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        log.info("修改人详情,{}",empToken);
        employeeService.updateEmpInfo(empInfoUpdateDTO,empToken);
        return R.ok("修改成功");
    }

    @GetMapping("/page")
    public R<PageVO> page(EmpPageQueryDTO empPageQueryDTO){
        log.info("员工分页查询,{}",empPageQueryDTO);
        PageVO pageVO =employeeService.page(empPageQueryDTO);
        return R.ok("查询成功",pageVO);
    }

    @PutMapping("/updatePwd")
    public R<Void> updatePwd(@Validated @RequestBody UpdatePwdDTO updatePwdDTO,
                        HttpServletRequest request){
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        return employeeService.updatePwd(updatePwdDTO,empToken);
    }

    @PutMapping("/update/status")
    public R<Void> updateStatus(@Validated @RequestBody EmpStatusDTO empStatusDTO,
                                HttpServletRequest request){
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        employeeService.updateStatus(empStatusDTO,empToken);
        return R.ok("修改状态成功");
    }

    @DeleteMapping("/delete/{id}")
    public R<Void> deleteEmp(@PathVariable("id") Integer empId,HttpServletRequest request){
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        employeeService.deleteEmp(empId,empToken);
        return R.ok("删除员工成功");
    }

    @PostMapping("/add")
    public R<Void> addEmp(@Validated EmpDTO empDTO,HttpServletRequest request){
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        employeeService.addEmp(empDTO,empToken);
        return R.ok("添加成功");
    }

    @GetMapping("/{id}")
    public R<EmpUpdateVO> getEmp(@PathVariable("id") Integer empId){
        return R.ok("查询成功",employeeService.getByEmpId(empId));
    }

    @PutMapping("/edit")
    public R<Void> editEmp(@Validated EmpUpdateDTO empUpdateDTO,HttpServletRequest request){
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        employeeService.editEmp(empUpdateDTO,empToken);
        return R.ok("修改成功");
    }
}
